package com.octopus.kernal.util.generate;

import java.io.File;
import java.io.FileWriter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.octopus.kernal.util.xml.*;

public class PropertiesGenerator {
	
	private static Log log = LogFactory.getLog(PropertiesGenerator.class);
	
	@SuppressWarnings("unused")
	private BusinessModel businessModel;
	
	private String outputPath;
		
	public PropertiesGenerator(String basePath, BusinessModel businessModel){
		this.businessModel = businessModel;
		this.outputPath = basePath + "/src/main/resources/";
	}
	
	public void generateI18nSampleProperties(BusinessModel model) throws Exception{
		String fileName = "sample-i18n.properties";
		String filePath = outputPath + "/" + fileName;
		File file = new File(filePath);
		if(!file.exists())
			file.createNewFile();
		FileWriter writer = new FileWriter(filePath);
		try{
			// Common
			writer.write("##############################################");							writer.write(System.getProperty("line.separator"));
			writer.write("# Common");																writer.write(System.getProperty("line.separator"));
			writer.write("##############################################");							writer.write(System.getProperty("line.separator"));
			writer.write("");																		writer.write(System.getProperty("line.separator"));
			writer.write("#######################");												writer.write(System.getProperty("line.separator"));
			writer.write("# Action");																writer.write(System.getProperty("line.separator"));
			writer.write("#######################");												writer.write(System.getProperty("line.separator"));
			writer.write("create=Create");															writer.write(System.getProperty("line.separator"));
			writer.write("edit=Edit");																writer.write(System.getProperty("line.separator"));
			writer.write("view=View");																writer.write(System.getProperty("line.separator"));
			writer.write("delete=Delete");															writer.write(System.getProperty("line.separator"));
			writer.write("");																		writer.write(System.getProperty("line.separator"));
			writer.write("#######################");												writer.write(System.getProperty("line.separator"));
			writer.write("# Label");																writer.write(System.getProperty("line.separator"));
			writer.write("#######################");												writer.write(System.getProperty("line.separator"));
			writer.write("label.common.chinese=\u4E2D\u6587");										writer.write(System.getProperty("line.separator"));
			writer.write("label.common.keyword=Keyword");											writer.write(System.getProperty("line.separator"));
			writer.write("label.common.search=Search");												writer.write(System.getProperty("line.separator"));
			writer.write("label.common.submit=Submit");												writer.write(System.getProperty("line.separator"));
			writer.write("label.common.save=Save");													writer.write(System.getProperty("line.separator"));
			writer.write("label.common.save.successfully=Save Successfully!");						writer.write(System.getProperty("line.separator"));
			writer.write("label.common.cancel=Cancel");												writer.write(System.getProperty("line.separator"));
			writer.write("label.common.back=Back");													writer.write(System.getProperty("line.separator"));
			writer.write("label.common.home=Home");													writer.write(System.getProperty("line.separator"));
			writer.write("label.common.homepage=HomePage");											writer.write(System.getProperty("line.separator"));
			writer.write("label.common.home.back=Back to Home");									writer.write(System.getProperty("line.separator"));
			writer.write("label.common.menu.hide=Hide Menu");										writer.write(System.getProperty("line.separator"));
			writer.write("label.common.menu.show=Show Menu");										writer.write(System.getProperty("line.separator"));
			writer.write("label.common.action=Actions");											writer.write(System.getProperty("line.separator"));
			writer.write("label.common.action.submit=Submit");										writer.write(System.getProperty("line.separator"));
			writer.write("label.common.action.create=Create");										writer.write(System.getProperty("line.separator"));
			writer.write("label.common.action.edit=Edit");											writer.write(System.getProperty("line.separator"));
			writer.write("label.common.action.delete=Delete");										writer.write(System.getProperty("line.separator"));
			writer.write("label.common.name=Name");													writer.write(System.getProperty("line.separator"));
			writer.write("label.common.description=Description");									writer.write(System.getProperty("line.separator"));
			writer.write("label.common.first=First");												writer.write(System.getProperty("line.separator"));
			writer.write("label.common.previous=Previous");											writer.write(System.getProperty("line.separator"));
			writer.write("label.common.next=Next");													writer.write(System.getProperty("line.separator"));
			writer.write("label.common.last=Last");													writer.write(System.getProperty("line.separator"));
			writer.write("label.common.total=Total");												writer.write(System.getProperty("line.separator"));
			writer.write("label.common.unauthorized=You have no permission to visit this page.");	writer.write(System.getProperty("line.separator"));
			writer.write("label.common.backto=Back to");											writer.write(System.getProperty("line.separator"));
			writer.write("label.common.from=From");													writer.write(System.getProperty("line.separator"));
			writer.write("label.common.to=To");														writer.write(System.getProperty("line.separator"));
			writer.write("label.common.print=Print");												writer.write(System.getProperty("line.separator"));
			writer.write("label.common.export=Export");												writer.write(System.getProperty("line.separator"));
			writer.write("label.common.date=Date");													writer.write(System.getProperty("line.separator"));
			writer.write("label.common.confirm.delete=Do you really want to delete?");				writer.write(System.getProperty("line.separator"));
			writer.write("label.common.yes=Yes");													writer.write(System.getProperty("line.separator"));
			writer.write("label.common.no=No");														writer.write(System.getProperty("line.separator"));
			writer.write("label.common.please.input=Please Input:");								writer.write(System.getProperty("line.separator"));
			writer.write("label.common.value.notexist=Value does not exist.");						writer.write(System.getProperty("line.separator"));
			writer.write("");																		writer.write(System.getProperty("line.separator"));
			writer.write("label.login.login=Login");												writer.write(System.getProperty("line.separator"));
			writer.write("label.login.logout=Logout");												writer.write(System.getProperty("line.separator"));
			writer.write("label.login.logged=Logged in as");										writer.write(System.getProperty("line.separator"));
			writer.write("label.login.haveaccount=Don't have an account?");							writer.write(System.getProperty("line.separator"));
			writer.write("label.login.comment=* Users created through the signup form have the role \"user\".  You can also log in as admin/admin, which has the \"admin\" role.");		writer.write(System.getProperty("line.separator"));
			writer.write("label.login.rememberme=Remember Me");										writer.write(System.getProperty("line.separator"));
			writer.write("label.login.username=Username");											writer.write(System.getProperty("line.separator"));
			writer.write("label.login.password=Password");											writer.write(System.getProperty("line.separator"));
			writer.write("label.login.signup=Signup");												writer.write(System.getProperty("line.separator"));
			writer.write("");																		writer.write(System.getProperty("line.separator"));
			writer.write("#######################");												writer.write(System.getProperty("line.separator"));
			writer.write("# Error");																writer.write(System.getProperty("line.separator"));
			writer.write("#######################");												writer.write(System.getProperty("line.separator"));
			writer.write("error.common.model.exist=The entity has already existed.");				writer.write(System.getProperty("line.separator"));
			writer.write("error.common.field.empty=Please specify a value for {0}.");				writer.write(System.getProperty("line.separator"));
			writer.write("error.common.field.exist=The value of {0} has already existed.");			writer.write(System.getProperty("line.separator"));
			writer.write("error.common.field.invalid=Please enter a valid value for {0}.");			writer.write(System.getProperty("line.separator"));
			writer.write("error.common.field.notnumber=Please enter a valid number for {0}.");		writer.write(System.getProperty("line.separator"));
			writer.write("error.common.field.length.greater=Please specify a valid value for {0}, not greater than {1}");		writer.write(System.getProperty("line.separator"));
			writer.write("error.common.field.length.noequal=Please specify a valid value for {0}, equals to {1}");				writer.write(System.getProperty("line.separator"));
			writer.write("error.login.invalid=Invalid username or password.  Please try again.");								writer.write(System.getProperty("line.separator"));
			writer.write("");																		writer.write(System.getProperty("line.separator"));
			writer.write("typeMismatch=The field type is mismatched.");								writer.write(System.getProperty("line.separator"));
			writer.write("");																		writer.write(System.getProperty("line.separator"));
			writer.write("#######################");												writer.write(System.getProperty("line.separator"));
			writer.write("# Single Select");														writer.write(System.getProperty("line.separator"));
			writer.write("#######################");												writer.write(System.getProperty("line.separator"));
			writer.write("");																		writer.write(System.getProperty("line.separator"));
			writer.write("");																		writer.write(System.getProperty("line.separator"));
			writer.write("");																		writer.write(System.getProperty("line.separator"));
			// Entity
			writer.write("##############################################");							writer.write(System.getProperty("line.separator"));
			writer.write("# Entity");																writer.write(System.getProperty("line.separator"));
			writer.write("##############################################");							writer.write(System.getProperty("line.separator"));
			for(Entity entity : model.getEntities()){
				writer.write("");																	writer.write(System.getProperty("line.separator"));
				writer.write("#######################");											writer.write(System.getProperty("line.separator"));
				writer.write("# " + entity.getAlias());												writer.write(System.getProperty("line.separator"));
				writer.write("#######################");											writer.write(System.getProperty("line.separator"));
				writer.write("label." + entity.getAlias() + ".model=");								writer.write(System.getProperty("line.separator"));
				writer.write("label." + entity.getAlias() + ".title=");								writer.write(System.getProperty("line.separator"));
				writer.write("label." + entity.getAlias() + ".comment=");							writer.write(System.getProperty("line.separator"));
				for(Field field : entity.getFields()){
					writer.write("label." + entity.getAlias() + ".field." + field.getName() + "=");	writer.write(System.getProperty("line.separator"));
				}
				writer.write("label." + entity.getAlias() + ".action.listall=");					writer.write(System.getProperty("line.separator"));
				writer.write("label." + entity.getAlias() + ".action.create=");						writer.write(System.getProperty("line.separator"));
				writer.write("label." + entity.getAlias() + ".keyword.tips=");						writer.write(System.getProperty("line.separator"));
			}
		}catch(Exception e){
			log.equals("Generate page error:" + this.outputPath + "/" + fileName);
			throw e;
		}finally{
	        writer.close(); 
		}
	}

}
